package com.gzist.blog_system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzist.blog_system.domain.Article;
import com.gzist.blog_system.domain.Comment;
import com.gzist.blog_system.domain.result.PageBean;
import com.gzist.blog_system.domain.vo.CommentPageVO;
import com.gzist.blog_system.mapper.CommentMapper;
import com.gzist.blog_system.service.CommentService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
@Transactional
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
        implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Override
    public PageBean<Comment> queryPage(CommentPageVO commentPageVO) {
        // 准备接受数据载体
        PageBean<Comment> pb = new PageBean<>();
        Page<Comment> page = new Page<>(commentPageVO.getPageNum(), commentPageVO.getPageSize());
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<Comment>().eq("article_id", commentPageVO.getArticleId());
        commentMapper.selectPage(page, queryWrapper);
        long total = page.getTotal();
        List<Comment> records = page.getRecords();
        pb.setTotal(total);
        pb.setItems(records);
        return pb;
    }

    @Override
    public List<Comment> getNewComment() {
        return commentMapper.getNewComment();
    }

    @Override
    public boolean pushComment(Comment comment) {
        return commentMapper.insert(comment) == 1;
    }

    @Override
    public Long countComment() {
        return commentMapper.selectCount(null);
    }

    @Override
    public boolean deleteComment(Long id) {
        return commentMapper.delete(new QueryWrapper<Comment>().eq("article_id", id)) == 1;
    }
}




